var tls = require('tls'),
	fs = require('fs'),
	sys = require('sys'),
	http = require('http'),
	url = require('url');

// Tls Server
var options = {
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('cert.pem'),
    port: 8000
};

var db = require('./server-db');

tls.createServer(options, function (socket)
{
    sys.puts("Tls connection established from: " + socket.remoteAddress);

    socket.addListener("data", function (data)
    { 
        sys.puts("Data received: " + data);

    	data = JSON.parse(data);

    	data.address = socket.remoteAddress;

    	data.__port = socket.port;

		db[data.action](data, function(result){
			
			socket.write(JSON.stringify(result));
		});	
    });
}).listen(8000, function()
{
	sys.puts("Tls server started.");
});
